<template>
    <div style="position:relative;width: 100%;height: 100%;">
        <div id="container" style="width: 100%;height: 100%;">
            <el-button type="success" style="position:absolute;z-index: 999;right: 5px;top: 5px;" @click="sendInfo">确认</el-button>
        </div>
    </div>
</template>

<script>
    const AMap = window.AMap;
    let map;
    let marker;
    let geocoder;
    export default {
        name: "ShowMap",
        props:{
           mapObj:{
               type:Array
           }
        },
        data(){
            return{
                point:[],
                address:""
            }
        },
        methods:{
            mapLoad() {
                map = new AMap.Map("container", {
                    zoom: 20,
                    pitch: 40,
                    mapStyle:"amap://styles/13d07c471c9b16780eee5a466867bf2a",
                    viewMode: '3D',
                    resizeEnable: false,
                });
                map.on("click",this.clickMap);
                geocoder = new AMap.Geocoder({
                    radius: 500, //范围，默认：1000,
                    batch:false,
                    extensions:"all"
                });
                let startIcon = new AMap.Icon({
                    // 图标尺寸
                    size: new AMap.Size(40, 40),
                    // 图标的取图地址
                    image:require('../../assets/stopIcon.png'),
                    // 图标所用图片大小
                    imageSize: new AMap.Size(40, 40),
                });
                marker = new AMap.Marker({
                    icon:startIcon,
                    offset: new AMap.Pixel(-13, -30),
                    // 设置是否可以拖拽
                    draggable: true,
                    cursor: 'move'
                    // 设置拖拽效果
                    // raiseOnDrag: true,
                });
                if (this.mapObj!=null){
                    map.setCenter(this.mapObj);
                    this.getAddress(this.mapObj);
                    map.add(marker);
                }
            },
            clickMap(e){
                let self = this;
                map.clearMap();
                let { lng, lat } = e.lnglat;
                this.point = [lng,lat];
                marker.setMap(map);
                self.getAddress(this.point);
                marker.on("dragend",function (e) {
                    let { lng, lat } = e.lnglat;
                    self.point = [lng,lat];
                    self.getAddress(self.point);
                });
                map.setFitView()

            },
            getAddress(data){
                let self = this;
                marker.setPosition(data);
                geocoder.getAddress(data, (status, result) =>{
                    if (status === 'complete' && result.info === 'OK') {
                        self.address = result.regeocode.formattedAddress;
                        marker.setLabel({
                            offset: new AMap.Pixel(40, 0),  //设置文本标注偏移量
                            content: "<p style='font-style: italic'> 地址: "+self.address+"</p>", //设置文本标注内容
                            direction: 'right' //设置文本标注方位
                        });
                    }else {
                        self.$message.warning("地址获取失败，请重试!")
                    }
                });

            },
            sendInfo(){
                let obj = {
                    address: this.address,
                    position: this.point
                };
                this.$emit("send-info",obj);
            }

        },
        mounted(){
          this.mapLoad();
        }
    }
</script>

<style>
    .amap-marker-label {
        position: absolute;
        z-index: 2;
        border: none !important;
        white-space: nowrap;
        cursor: pointer;
        padding: 3px;
        font-size: 12px;
        line-height: 14px;

    }
</style>